﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;

namespace YArchitech.Plumbing
{
	public partial class HotMaxUsedWater : System.Windows.Forms.Form
	{
		public HotMaxUsedWater()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = (base.Icon = Resource.YArchitechIcon);
			this.InitCalcuList();
			this.calculateOjects = new List<HotUsedWaterCal>();
			if (HotUsedWaterCalManage.Instance.bIsCalForDay)
			{
				this.radioDay.Checked = true;
			}
			else
			{
				this.radioHour.Checked = true;
			}
			this.txtHotTemp.Text = Math.Round(HotUsedWaterCalManage.Instance.HotTemp, 1).ToString();
			this.txtColdTemp.Text = Math.Round(HotUsedWaterCalManage.Instance.ColdTemp, 1).ToString();
			this.szTemp = this.txtHotTemp.Text + this.txtColdTemp.Text;
			base.CancelButton = this.buttonCancel;
		}

		private void InitCalcuList()
		{
			this.CalcuResultList.Clear();
			if (HotUsedWaterCalManage.Instance.bIsCalForDay)
			{
				this.label2.Enabled = true;
				this.txtHotTemp.Enabled = true;
				this.CalcuResultList.Columns.Add("", 0, HorizontalAlignment.Center);
				this.CalcuResultList.Columns.Add("建筑物名称", 200, HorizontalAlignment.Left);
				this.CalcuResultList.Columns.Add("高日用水定额", 100, HorizontalAlignment.Center);
				this.CalcuResultList.Columns.Add("使用时间(h)", 80, HorizontalAlignment.Center);
				this.CalcuResultList.Columns.Add("数 量", 50, HorizontalAlignment.Center);
				this.CalcuResultList.Columns.Add("单 位", 50, HorizontalAlignment.Center);
				this.CalcuResultList.Columns.Add("小时变化系数", 100, HorizontalAlignment.Center);
			}
			else
			{
				this.label2.Enabled = false;
				this.txtHotTemp.Enabled = false;
				this.CalcuResultList.Columns.Add("", 0, HorizontalAlignment.Center);
				this.CalcuResultList.Columns.Add("用水部位名称", 200, HorizontalAlignment.Left);
				this.CalcuResultList.Columns.Add("小时水量(L)", 100, HorizontalAlignment.Center);
				this.CalcuResultList.Columns.Add("水温(℃)", 100, HorizontalAlignment.Center);
				this.CalcuResultList.Columns.Add("数 量", 70, HorizontalAlignment.Center);
				this.CalcuResultList.Columns.Add("同时百分百", 100, HorizontalAlignment.Center);
			}
			this.CalcuResultList.notification = new EidtListView.DataChangedNotification(this.changeDataManually);
			this.calculateOjects = new List<HotUsedWaterCal>();
			this.txtHeat.Text = "0.00";
			this.txtWater.Text = "0.00";
		}

		private void AddType_Click(object sender, EventArgs e)
		{
			HotWaterTypes hotWaterTypes = new HotWaterTypes();
			hotWaterTypes.ShowDialog(this);
			HotUsedWaterCal calculate = hotWaterTypes.calculate;
			if (calculate != null)
			{
				double usedCount = calculate.usedCount;
				this.calculateOjects.Add(calculate);
				this.UpdateResultList();
				this.bIsCal = false;
			}
		}

		private void UpdateResultList()
		{
			this.CalcuResultList.Items.Clear();
			for (int i = 0; i < this.calculateOjects.Count; i++)
			{
				HotUsedWaterCal hotUsedWaterCal = this.calculateOjects[i];
				ListViewItem listViewItem = new ListViewItem();
				listViewItem.Text = "";
				listViewItem.Tag = i;
				ListViewItem.ListViewSubItem listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = hotUsedWaterCal.typeName;
				listViewItem.SubItems.Add(listViewSubItem);
				listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = hotUsedWaterCal.maxUsedProposal.ToString();
				listViewItem.SubItems.Add(listViewSubItem);
				if (HotUsedWaterCalManage.Instance.bIsCalForDay)
				{
					listViewSubItem = new ListViewItem.ListViewSubItem();
					listViewSubItem.Text = hotUsedWaterCal.usedTimeProposal.ToString();
					listViewItem.SubItems.Add(listViewSubItem);
					listViewSubItem = new ListViewItem.ListViewSubItem();
					listViewSubItem.Text = hotUsedWaterCal.usedCount.ToString();
					listViewSubItem.Tag = 3;
					listViewItem.SubItems.Add(listViewSubItem);
					listViewSubItem = new ListViewItem.ListViewSubItem();
					listViewSubItem.Text = hotUsedWaterCal.unit;
					listViewItem.SubItems.Add(listViewSubItem);
				}
				else
				{
					listViewSubItem = new ListViewItem.ListViewSubItem();
					listViewSubItem.Text = hotUsedWaterCal.usedTempProposal.ToString();
					listViewItem.SubItems.Add(listViewSubItem);
					listViewSubItem = new ListViewItem.ListViewSubItem();
					listViewSubItem.Text = hotUsedWaterCal.usedCount.ToString();
					listViewSubItem.Tag = 3;
					listViewItem.SubItems.Add(listViewSubItem);
				}
				listViewSubItem = new ListViewItem.ListViewSubItem();
				listViewSubItem.Text = hotUsedWaterCal.coefficientProposal.ToString();
				listViewSubItem.Tag = 4;
				listViewItem.SubItems.Add(listViewSubItem);
				this.CalcuResultList.Items.Add(listViewItem);
			}
		}

		private void WriteResultToFile_Click(object sender, EventArgs e)
		{
			try
			{
				if (!this.bIsCal || this.szTemp != this.txtHotTemp.Text + this.txtColdTemp.Text)
				{
					DialogResult dialogResult = MessageBox.Show("计算内容有改变，是否需要先计算再出计算书？", "盈建科提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
					if (dialogResult == DialogResult.Yes)
					{
						this.Calculate();
						this.bIsCal = true;
						this.szTemp = this.txtHotTemp.Text + this.txtColdTemp.Text;
					}
					else if (dialogResult != DialogResult.No)
					{
						return;
					}
				}
				HotUsedWaterCalManage.Instance.SetDefaults();
				SaveFileDialog saveFileDialog = new SaveFileDialog();
				if (HotUsedWaterCalManage.Instance.bIsCalForDay)
				{
					saveFileDialog.FileName = "集中供应-热水小时用水量计算书";
				}
				else
				{
					saveFileDialog.FileName = "定时供应-热水小时用水量计算书";
				}
				saveFileDialog.Filter = "Word 97-2003 文档|*.doc";
				saveFileDialog.FilterIndex = 1;
				saveFileDialog.RestoreDirectory = true;
				try
				{
					if (saveFileDialog.ShowDialog() == DialogResult.OK)
					{
						string text = saveFileDialog.FileName.ToString();
						new HotWaterCalBook().Go(text, this.calculateOjects, this.txtHeat.Text, this.txtWater.Text);
						if (MessageBox.Show("是否现在查看？", "盈建科提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
						{
							Process.Start(text);
						}
					}
				}
				catch (Exception ex)
				{
					YJKMessageBox.Show(ex.Message + "请更换一下另存文件的名称或者先关闭此文件");
				}
			}
			catch (Exception ex2)
			{
				YJKMessageBox.Error(ex2.ToString());
			}
		}

		private ExcelData GetExcelData()
		{
			List<ExcelData.Cell> list = new List<ExcelData.Cell>();
			List<List<ExcelData.Cell>> list2 = new List<List<ExcelData.Cell>>();
			for (int i = 0; i < this.CalcuResultList.Columns.Count; i++)
			{
				if (i != 0)
				{
					list.Add(new ExcelData.Cell
					{
						TypeOfCell = ExcelData.ExcelCellType.StringCell,
						Data = this.CalcuResultList.Columns[i].Text
					});
				}
			}
			for (int j = 0; j < this.CalcuResultList.Items.Count; j++)
			{
				List<ExcelData.Cell> list3 = new List<ExcelData.Cell>();
				list3.Add(new ExcelData.Cell
				{
					TypeOfCell = ExcelData.ExcelCellType.StringCell,
					Data = this.CalcuResultList.Items[j].SubItems[1].Text
				});
				string text = this.CalcuResultList.Items[j].SubItems[2].Text;
				ExcelData.Cell cell = new ExcelData.Cell();
				if (string.IsNullOrEmpty(text))
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.StringCell;
				}
				else
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.doubleCell;
					cell.Round = 2;
				}
				cell.Data = text;
				list3.Add(cell);
				list3.Add(new ExcelData.Cell
				{
					TypeOfCell = ExcelData.ExcelCellType.StringCell,
					Data = this.CalcuResultList.Items[j].SubItems[3].Text
				});
				text = this.CalcuResultList.Items[j].SubItems[4].Text;
				cell = new ExcelData.Cell();
				if (string.IsNullOrEmpty(text))
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.StringCell;
				}
				else
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.doubleCell;
					cell.Round = 2;
				}
				cell.Data = text;
				list3.Add(cell);
				text = this.CalcuResultList.Items[j].SubItems[5].Text;
				cell = new ExcelData.Cell();
				if (string.IsNullOrEmpty(text))
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.StringCell;
				}
				else
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.doubleCell;
					cell.Round = 2;
				}
				cell.Data = text;
				list3.Add(cell);
				text = this.CalcuResultList.Items[j].SubItems[6].Text;
				cell = new ExcelData.Cell();
				if (string.IsNullOrEmpty(text))
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.StringCell;
				}
				else
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.doubleCell;
					cell.Round = 1;
				}
				cell.Data = text;
				list3.Add(cell);
				text = this.CalcuResultList.Items[j].SubItems[7].Text;
				cell = new ExcelData.Cell();
				if (string.IsNullOrEmpty(text))
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.StringCell;
				}
				else
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.doubleCell;
					cell.Round = 2;
				}
				cell.Data = text;
				list3.Add(cell);
				text = this.CalcuResultList.Items[j].SubItems[8].Text;
				cell = new ExcelData.Cell();
				if (string.IsNullOrEmpty(text))
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.StringCell;
				}
				else
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.doubleCell;
					cell.Round = 2;
				}
				cell.Data = text;
				list3.Add(cell);
				text = this.CalcuResultList.Items[j].SubItems[9].Text;
				cell = new ExcelData.Cell();
				if (string.IsNullOrEmpty(text))
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.StringCell;
				}
				else
				{
					cell.TypeOfCell = ExcelData.ExcelCellType.doubleCell;
					cell.Round = 2;
				}
				cell.Data = text;
				list3.Add(cell);
				list2.Add(list3);
			}
			return new ExcelData
			{
				SheetName = "最高日最高时用水量计算",
				FileName = "最高日最高时用水量计算",
				Title = "最高日最高时用水量",
				ColumnsData = list,
				RowsData = list2
			};
		}

		private void buttonDelete_Click(object sender, EventArgs e)
		{
			for (int i = this.CalcuResultList.SelectedItems.Count - 1; i >= 0; i--)
			{
				ListViewItem listViewItem = this.CalcuResultList.SelectedItems[i];
				if (listViewItem.Tag != null && (int)listViewItem.Tag >= 0 && (int)listViewItem.Tag <= this.calculateOjects.Count - 1)
				{
					this.calculateOjects.RemoveAt((int)listViewItem.Tag);
					listViewItem.Remove();
				}
				this.bIsCal = false;
			}
			this.UpdateResultList();
		}

		private void changeDataManually(string text, int itemTag, int subItemTag)
		{
			bool flag = false;
			double num = 0.0;
			try
			{
				num = Convert.ToDouble(text);
				flag = true;
			}
			catch (Exception ex)
			{
				ex.ToString();
			}
			if (flag && itemTag > -1 && itemTag < this.calculateOjects.Count)
			{
				HotUsedWaterCal hotUsedWaterCal = this.calculateOjects[itemTag];
				if (subItemTag != 3)
				{
					if (subItemTag == 4)
					{
						hotUsedWaterCal.coefficientProposal = num;
					}
				}
				else
				{
					hotUsedWaterCal.usedCount = num;
				}
				this.UpdateResultList();
				this.bIsCal = false;
			}
		}

		private void buttonClear_Click(object sender, EventArgs e)
		{
			this.CalcuResultList.Items.Clear();
			this.calculateOjects.Clear();
			this.txtHeat.Text = "0.00";
			this.txtWater.Text = "0.00";
		}

		private void buttonCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void buttonCalculate_Click(object sender, EventArgs e)
		{
			this.Calculate();
			HotUsedWaterCalManage.Instance.SetDefaults();
			this.bIsCal = true;
			this.szTemp = this.txtHotTemp.Text + this.txtColdTemp.Text;
		}

		private bool Calculate()
		{
			bool flag = false;
			try
			{
				HotUsedWaterCalManage.Instance.HotTemp = Convert.ToDouble(this.txtHotTemp.Text);
				HotUsedWaterCalManage.Instance.ColdTemp = Convert.ToDouble(this.txtColdTemp.Text);
				if (HotUsedWaterCalManage.Instance.bIsCalForDay)
				{
					if (HotUsedWaterCalManage.Instance.HotTemp <= HotUsedWaterCalManage.Instance.ColdTemp)
					{
						MessageBox.Show(this, "热水温度应大于冷水温度！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
						return false;
					}
					if (Math.Abs(HotUsedWaterCalManage.Instance.HotTemp - HotUsedWaterCalManage.Instance.ColdTemp) < 1.0)
					{
						MessageBox.Show(this, "冷热水温度相差太小！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
						return false;
					}
				}
				else
				{
					foreach (HotUsedWaterCal hotUsedWaterCal in this.calculateOjects)
					{
						if (hotUsedWaterCal.usedTempProposal <= HotUsedWaterCalManage.Instance.ColdTemp)
						{
							MessageBox.Show(this, hotUsedWaterCal.typeName + "的水温应大于设计冷水温度！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
							return false;
						}
					}
				}
				flag = true;
			}
			catch (Exception ex)
			{
				ex.ToString();
				MessageBox.Show(this, "请输入一个有效的数字！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				return false;
			}
			if (flag)
			{
				double num = 0.0;
				for (int i = 0; i < this.calculateOjects.Count; i++)
				{
					HotUsedWaterCal hotUsedWaterCal2 = this.calculateOjects[i];
					hotUsedWaterCal2.Calculate();
					num += hotUsedWaterCal2.heatPerHour;
				}
				double num2 = 4.187;
				double num3 = 0.9832;
				double num4 = HotUsedWaterCalManage.Instance.HotTemp - HotUsedWaterCalManage.Instance.ColdTemp;
				double value = num / (num4 * num2 * num3);
				num = Math.Round(num, 2);
				value = Math.Round(value, 2);
				this.txtHeat.Text = num.ToString();
				this.txtWater.Text = value.ToString();
				this.UpdateResultList();
			}
			return true;
		}

		private void radioDay_CheckedChanged(object sender, EventArgs e)
		{
			if (this.radioDay.Checked)
			{
				HotUsedWaterCalManage.Instance.bIsCalForDay = true;
			}
			else
			{
				HotUsedWaterCalManage.Instance.bIsCalForDay = false;
			}
			this.InitCalcuList();
			this.txtHeat.Text = "0.00";
			this.txtWater.Text = "0.00";
			this.bIsCal = true;
			this.szTemp = this.txtHotTemp.Text + this.txtColdTemp.Text;
		}

		private void btnHelp_Click(object sender, EventArgs e)
		{
			new HotWaterTemp().ShowDialog();
		}

		private void CalcuResultList_MouseClick(object sender, MouseEventArgs e)
		{
			ListViewHitTestInfo listViewHitTestInfo = this.CalcuResultList.HitTest(e.X, e.Y);
			if (listViewHitTestInfo.Item == null || listViewHitTestInfo.SubItem == null)
			{
				return;
			}
			ListViewItem.ListViewSubItem subItem = listViewHitTestInfo.SubItem;
			if (subItem.Tag == null)
			{
				return;
			}
			int num = (int)subItem.Tag;
		}

		private List<HotUsedWaterCal> calculateOjects;

		private bool bIsCal = true;

		private string szTemp = "";
	}
}
